import json

import requests


def searxng(query: str):
    #浏览器可以访问这个出现搜索结果
    #http://localhost:8080/search?q=%E5%95%8A%E6%98%AF%E9%9D%9E%E5%BE%97%E5%A4%B1&format=json
    url = "http://localhost:8080/search"
    params = {"q": str(query), "format": "json"}
    response = requests.get(url, params=params)
    # print(response)
    results_dict = response.json()
    print(type(results_dict))
    print(json.dumps(results_dict, indent=4, ensure_ascii=False))
    results_list = results_dict.get("results", [])
    web_result = ""
    if results_list:
        for results_item in results_list[:5]:
            title = results_item.get("title", "")
            content = results_item.get("content", "")
            url = results_item.get("url", "")
            if title and content:
                web_result = (
                    web_result + "标题：" + title + "\n" + "内容：" + content + "\n\n"+"url:"+url+"\n\n"
                )
    return web_result


if __name__ == "__main__":
    res = searxng("开始的开始，我们都是孩子")
    print(res)